package net.sf.rose.business.action.define;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import json.JsonHttpRequestBean;
import json.JsonTreeBean;
import json.JsonHttpResponse;

import net.sf.rose.business.action.Download;
import net.sf.rose.business.bean.base.DownloadFileBean;
import net.sf.rose.business.bean.control.MenuBean;
import net.sf.rose.business.bean.control.judge.JudgeBean;
import net.sf.rose.business.bean.control.judge.JudgeStatement;
import net.sf.rose.business.service.MenuSevice;
import net.sf.rose.exception.SystemErrorCode;
import net.sf.rose.exception.SystemException;
import net.sf.rose.jdbc.UserBean;
import net.sf.rose.jdbc.service.Service;
import net.sf.rose.web.OnlineUserListener;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author fengjian
 * @since 2014/03/06
 */
@Controller
@RequestMapping("define/menu" )
public class MenuController {

	/**
	 * 
	 */
	@RequestMapping("/list.html")
	public ModelAndView list(HttpServletRequest request, HttpServletResponse response, Service service) {
		MenuBean group = MenuSevice.getService().getMenuBean();
		List<JsonTreeBean> list = new ArrayList<JsonTreeBean>();
		JsonTreeBean tree = new JsonTreeBean();
		tree.setId("root");
		tree.setName("根目录");
		tree.setExpanded(true);
		list.add(tree);
		group.setId("root");
		this.addChildMenu(group, list);
		JsonHttpResponse.renderJson(response, list);
		return null;
	}
	
	private void addChildMenu(MenuBean parent, List<JsonTreeBean> list) {
		for (MenuBean m : parent.getChildMenus()) {
			JsonTreeBean tree = new JsonTreeBean();
			tree.setId(m.getId());
			tree.setName(m.getText());
			tree.setPid(parent.getId());
			list.add(tree);
			if (m.getChildMenus().size() > 0) {
				this.addChildMenu(m, list);
			}
		}
	}

	/**
	 * 
	 */
	@RequestMapping("/get.html")
	public void get(HttpServletRequest request, HttpServletResponse response, String id) {
		MenuBean bean = MenuSevice.getService().getMenuBean(id);
		JsonHttpResponse.renderJson(response, (Object) bean);
	}

	/**
	 * 
	 */
	@RequestMapping("/save.html")
	public void save(HttpServletRequest request, HttpServletResponse response, String menuID,String menu, String judge) {
		JsonHttpRequestBean json = new JsonHttpRequestBean();
		MenuBean menuBean = (MenuBean) json.parseJsonBean(menu, MenuBean.class);
		if (!judge.equals("[]")) {
			List<Object> judges = json.parseJsonList(judge, JudgeStatement.class);
			JudgeBean jb = new JudgeBean();
			for (Object obj : judges) {
				jb.addJudge((JudgeStatement) obj);
			}
			menuBean.setJudge(jb);
		}
		MenuBean bean = MenuSevice.getService().getMenuBean(menuID);
		if (menuBean.getId() == null) {
			bean.addChildMenu(menuBean);
		} else {
			bean.update(menuBean);
		}
		JsonHttpResponse.renderMessage(response, "ok");
	}

	/**
	 * 
	 */
	@RequestMapping("/delete.html")
	public void delete(HttpServletRequest request, HttpServletResponse response, String name) {
		MenuSevice service = MenuSevice.getService();
		MenuBean menu = service.getMenuBean(name);
		MenuBean parent = service.getParent(menu);
		parent.removeChildMenus(name);
		JsonHttpResponse.renderMessage(response, "ok");
	}

	/**
	 * 
	 */
	@RequestMapping("/saveAll.html")
	public void saveAll(HttpServletRequest request, HttpServletResponse response) {
		UserBean user = OnlineUserListener.getOnlineUser(request);
		if (user == null) {
			user = new UserBean();
			user.setUserName("system");
		}
		MenuSevice.save(user);
		JsonHttpResponse.renderMessage(response, "ok");
	}

	/**
	 * 提交操作
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/input.html")
	public ModelAndView input(HttpServletRequest request) {
		Object obj = request.getSession().getAttribute(Download.TEMP_FILES);
		if (obj == null) {
			throw new SystemException("Session中找不到上传的文件。", SystemErrorCode.A_PRARAMTER_LOST_ERROR);
		}
		Map<String, DownloadFileBean> map = (Map<String, DownloadFileBean>) obj;
		InputStream in = null;
		try {
			for (DownloadFileBean bean : map.values()) {
				if (bean.getData() != null) {
					in = bean.getData().getInputStream();
				}
			}
		} catch (IOException e) {
			throw new SystemException("上传的文件读取错误 !", SystemErrorCode.A_PRARAMTER_LOST_ERROR);
		}
		if (in == null) {
			throw new SystemException("没有找到上传的文件 !", SystemErrorCode.A_PRARAMTER_LOST_ERROR);
		}
		MenuSevice.input(in);
		ModelAndView mav = null;
		mav = new ModelAndView("ajaxDone");
		mav.addObject("statusCode", 200);
		mav.addObject("message", "导入成功！");
		mav.addObject("callbackType", true);
		return mav;
	}

	/**
	 * 提交操作
	 */
	@RequestMapping("/output.html")
	public String output(HttpServletRequest request) {
		File file = MenuSevice.output();
		request.setAttribute(Download.DOWNLOAD_FILE, file);
		request.setAttribute(Download.DOWNLOAD_FILE_NAME, "菜单配置.xml");
		return "forward:/download.html";
	}
}
